﻿using System;
using System.Windows;
using System.Windows.Controls;
using Mss.Common.Interfaces;

namespace Mss.UiCommon.Controls
{
    public class View : ContentControl, IView
    {

        #region Events
        
        public event EventHandler Showing;
        public event EventHandler Hidding;
        public event EventHandler Closing;

        #endregion

        #region Constructor

        static View()
        {
            //Apply default style.
            DefaultStyleKeyProperty.OverrideMetadata(typeof(View),
                                                     new FrameworkPropertyMetadata(typeof(View)));
        }

        #endregion

        #region Properties

        public bool IsRegistered
        {
            get; set;
        }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <filterpriority>2</filterpriority>
        public virtual void Dispose()
        {
            Content = null;

            Showing = null;
            Hidding = null;
            Closing = null;
        }

        #endregion

        #region Methods
        
        /// <summary>
        /// Show View.
        /// </summary>
        public void Show()
        {
            Visibility = Visibility.Visible;
            OnShow();
        }

        /// <summary>
        /// Hide View.
        /// </summary>
        public void Hide()
        {
            OnHide();
            Visibility = Visibility.Collapsed;
        }

        /// <summary>
        /// Close View.
        /// </summary>
        public void Close()
        {
            OnClose();
            Dispose();
        }

        protected virtual void OnShow()
        {
            if (Showing != null)
            {
                Showing.Invoke(this, EventArgs.Empty);
            }
        }

        protected virtual void OnHide()
        {
            if (Hidding != null)
            {
                Hidding.Invoke(this, EventArgs.Empty);
            }
        }

        protected virtual void OnClose()
        {
            if (Closing != null)
            {
                Closing.Invoke(this, EventArgs.Empty);
            }
        }

        #endregion
    }
}